package leetcode

import "fmt"

func minSumOfLengths(arr []int, target int) int {
	min1,min2 := 100000,100000

	s := 0
	l := len(arr)
	lastEnd := -1
	lastMin := 0
	for i:=0;i<l;i++ {
		start := i
		for s < target && start < l {
			s += arr[start]
			start++
		}
		if s == target {
			v := start-i
			if lastEnd != -1 {
				if lastEnd >= i {
					if lastMin <= v {
						s = 0
						continue
					}
				}
			}
			fmt.Println(lastEnd, lastMin, v, i)
			fmt.Println(fmt.Sprintf("start %d,i %d,min %d, min %d",start, i, min1, min2))
			min1,min2 = setMin(min1, min2, v)
			fmt.Println(fmt.Sprintf("start %d,i %d,min %d, min %d",start, i, min1, min2))
			lastMin = v
			lastEnd = start-1
		}
		s = 0
	}
	if min1+min2 > 100000 {
		return -1
	}
	return min1+min2
}

func setMin(a,b,c int) (int,int) {
	if a > b {
		a,b = b,a
	}
	if b > c {
		b = c
		return a,b
	}
	if a > c {
		a = c
		return a,b
	}
	return a, b
}